import math
import pandas as pd
import csv
from matplotlib import pyplot as plt

df = pd.read_csv("D:/桌面/iris (2).data")

# 打开文件并读取为列表
with open("D:/桌面/iris (2).data", 'r') as f:
    reader = csv.reader(f)
    data = list(reader)

# 转换为浮点型
data_float = [[float(x) for x in row[:4]] for row in data]
data_float = data_float[:-1]

# 计算每一列的均值和方差
means = []  # 均值
stds = []  # 方差


for i in range(4):
    col_values = [row[i] for row in data_float]
    means.append(sum(col_values) / len(col_values))

    variance = sum([(x - means[i])**2 for x in col_values]) / (len(col_values)-1)
    stds.append(variance**0.5)

print(f'Means:{means}')
print(f'Standard Deviations:{stds}')

print("---------------------------------------------->")
# 计算协方差和Pearson相关系数
def covariance(x, y):
    covar = 0.0
    for i in range(len(x)):
        covar += (x[i] - means[0]) * (y[i] - means[1])
    return covar / float(len(x) - 1)

def pearson(x, y):
    covar = covariance(x, y)
    corr = covar / (stds[0] * stds[1])
    return corr

# 计算每一对的关系 因为有四列，所以设四列的值
col_1 = [row[0] for row in data_float]
col_2 = [row[1] for row in data_float]
col_3 = [row[2] for row in data_float]
col_4 = [row[3] for row in data_float]

print("第一列和第二列的相关系数：", pearson(col_1, col_2))
print("第一列和第三列的相关系数：", pearson(col_1, col_3))
print("第一列和第四列的相关系数：", pearson(col_1, col_4))
print("第二列和第三列的相关系数：", pearson(col_2, col_3))
print("第二列和第四列的相关系数：", pearson(col_2, col_4))
print("第三列和第四列的相关系数：", pearson(col_3, col_4))

print("--------------------------------->")

# 制作高斯核

def gaosi(sigma, size):
    point = (size - 1) // 2
    cul = [[0 for x in range(size)] for y in range(size)]

    # 计算分布系数
    zoef = 1 / (2 * math.pi * sigma ** 2)

    # 计算高斯矩阵各个点的值
    total_sum = 0.0
    for i in range(-point, point + 1):
        for j in range(-point, point + 1):
            val = zoef * math.exp(-(i**2 + j**2) / (2*sigma ** 2))
            cul[i+point][j+point] = val
            total_sum += val

    # 归一化
    diga = round(1.0 / total_sum, ndigits=4)

    for i in range(size):
        for j in range(size):
            cul[i][j] *= diga

    return cul

Talo = gaosi(sigma=1.1, size=5)
for r in Talo:
    print([round(x, 2) for x in r])
plt.imshow(Talo, cmap='gray')
plt.colorbar()
plt.show()





